package demo.ejs.web;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Controller("demoAction")
@Results( { 
	@Result(name = "success",type="dispatcher" ,location = "/result.jsp"),
	@Result(name = "xmlEjs",type="xmlResult" ),
	@Result(name = "jsonEjs",type="jsonResult" ),
	@Result(name = "json",type="json" ),
	@Result(name = "updateResult",type="dispatcher" ,location = "/TestResult.xml" ),
	@Result(name = "tileDemo",type="tiles",location="tiles.home"),
	@Result(name = "viewPartnerResult",type="dispatcher" ,location = "/jsp/dashBoard.jsp"),
	})
@ParentPackage("myDefault")
public class DemoAction extends ActionSupport {
	String arg;
	String orgCode;
	private static final long serialVersionUID = 1L;
	
	@Override
	@Action("test")
	public String execute() throws Exception {
		System.out.println("Hello!");
//		int i=1/0;
		if(arg!=null && arg.equalsIgnoreCase("xml")){
			return "xmlEjs";
		}
		if(arg!=null && arg.equalsIgnoreCase("json")){
			return "jsonEjs";
		}
		return SUCCESS;
	}
	
	@Action("download")
	public String downloadData() throws Exception {
		System.out.println("Hello download!");
		if(arg!=null && arg.equalsIgnoreCase("xml")){
			return "xmlEjs";
		}
		if(arg!=null && arg.equalsIgnoreCase("json")){
			return "jsonEjs";
		}
		return SUCCESS;
	}
	@Action("upload")
	public String uploadData()  {
		System.out.println("Hello upload!");
		if(arg!=null && arg.equalsIgnoreCase("xml")){
			HttpServletRequest request = ServletActionContext.getRequest();
			String s=request.getParameter("Data");
			System.out.println(s);
//			return "updateResult";
		}
		if(arg!=null && arg.equalsIgnoreCase("json")){
			HttpServletRequest request = ServletActionContext.getRequest();
			String s=request.getParameter("Data");
			System.out.println(s);
//			return "updateResult";
		}
		return SUCCESS;
	}
	
	@Action("home")
	public String home() throws Exception {
		return "tileDemo";
	}
	
	@Action("viewPartner")
	public String viewPartner() throws Exception {
//		String ss=(String) ActionContext.getContext().getParameters().get("orgCode");
		System.out.println("orgCode:"+orgCode);
//		System.out.println("ss:"+ss);
		String s2=ServletActionContext.getRequest().getParameter("orgCode");
		System.out.println("s2:"+s2);
		return "viewPartnerResult";
	}
	
	
	
	
	
	public String getArg() {
		return arg;
	}

	public void setArg(String arg) {
		this.arg = arg;
	}
	public String getOrgCode() {
		return orgCode;
	}

	public void setOrgCode(String orgCode) {
		this.orgCode = orgCode;
	}
	
}
